package com.shoulder.boot.acl.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.shoulder.boot.acl.entity.Menu;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * 左侧菜单树
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class LeftMenuVo {

    /**
     * 左侧菜单树元数据信息
     */
    private LeftMetaVo meta;

    /**
     * 菜单id
     */
    private String id;

    /**
     * 父菜单id
     */
    private String parentId;

    /**
     * 菜单路由名称
     */
    private String name;

    /**
     * 菜单路径
     */
    private String path;

    /**
     * 子菜单
     */
    private List<LeftMenuVo> children;

    public static List<LeftMenuVo> toLeftVos(List<Menu> menus) {
        if (CollectionUtils.isEmpty(menus)) {
            return Collections.emptyList();
        }
        return menus.stream().map(LeftMenuVo::toLeftVo).toList();
    }

    public static LeftMenuVo toLeftVo(Menu menu) {
        LeftMenuVo menuVo = new LeftMenuVo();
        BeanUtils.copyProperties(menu, menuVo);
        LeftMetaVo metaVo = new LeftMetaVo();
        metaVo.setIcon(menu.getIcon());
        metaVo.setTitle(menu.getTitle());
        menuVo.setMeta(metaVo);
        return menuVo;
    }
}
